ChunkSaver = class()

function ChunkSaver:init()

end

function ChunkSaver:saveChunk(chunk,fileName)
    chunk.changed = false
    local t = self:generateChunkData(chunk)
    saveText(fileName,json.encode(t))
end

function ChunkSaver:readChunk(world,fileName)
    local date = readText(fileName)
    if date == nil then return nil end
    if date == "" then return nil end
    local t = json.decode(readText(fileName))
    if t.version ~= WorldSaver.Version then return nil end
    if t.seed ~= world.seed then return nil end
    return self:decodeChunkData(world,t)
end

function ChunkSaver:generateChunkData(chunk)
    local t = {
    seed = world.seed,
    version = WorldSaver.Version,
    x = chunk.x,
    z = chunk.z,
    blocks = chunk.blocks,
    heightMap = chunk.heightMap,
    brightness = chunk.brightness,
    biomeArray = chunk.biomeArray
    }
    return t
end

function ChunkSaver:decodeChunkData(world,t)
    local chunk = Chunk(world,t.x,t.z)
    chunk.heightMap = t.heightMap
    chunk.blocks = t.blocks
    chunk.brightness = t.brightness
    chunk.biomeArray = t.biomeArray
    
    chunk.changed = false
    return chunk
end